package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.vo.DishVoo;
import com.itheima.dto.DishFlavorDto;
import com.itheima.service.DishService;
import com.itheima.vo.DishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YangJiaHao
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public CommonResult page(String name,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Integer type) {
        PageInfo<DishVoo> pageInfo = dishService.page(name, page, pageSize, type);
        return CommonResult.success(pageInfo);
    }

    @PostMapping
    public CommonResult addDish(@RequestBody DishFlavorDto dishFlavorDto) {
        dishService.addDish(dishFlavorDto);
        return CommonResult.success("新增成功");
    }

    @PutMapping
    public CommonResult modifyDish(@RequestBody DishFlavorDto dishFlavorDto) {
        dishService.modifyDish(dishFlavorDto);
        return CommonResult.success("修改成功");
    }

    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable String id) {
        DishVo dishVo = dishService.getById(id);
        return CommonResult.success(dishVo);
    }

    @DeleteMapping
    public CommonResult delete(String ids) {
        dishService.delete(ids);
        return CommonResult.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public CommonResult status(@PathVariable Integer status , String ids){
        dishService.status(status,ids);
        return CommonResult.success("操作成功");
    }

    @GetMapping("/list")
    public CommonResult list(String categoryId) {
        List<DishVo> list = dishService.getList(categoryId);
        return CommonResult.success(list);
    }

}
